/*
 * ADS1115.cpp
 *
 *  Created on: Jun 17, 2024
 *      Author: tangst
 */

#include "ADS1115.h"
#include "main.h"
#include "gpio.h"
#include "tool.h"

void ADS1115_config(soft_i2c_driver *soft_i2c_driver, uint8_t channel)
{
  soft_i2c_WriteReg(soft_i2c_driver, ADS1115_REG_Config, ADS1115_Config_MSB | (channel << 4), ADS1115_Config_LSB);
 
}

uint16_t ADS1115_ReadADC(soft_i2c_driver *soft_i2c_driver)
{
  return soft_i2c_ReadReg(soft_i2c_driver,ADS1115_REG_Conversion);
}

void ADS1115_ReadAllChannel(soft_i2c_driver *soft_i2c_driver, uint16_t data[])
{
  for(uint8_t i=0; i<4; i++)
  {
    ADS1115_config(soft_i2c_driver,i);
    HAL_Delay(10);
    data[i] = ADS1115_ReadADC(soft_i2c_driver);
  }
}

void ADS1115_VolConversion(float *vol_value,int16_t data[4],float base_vol)
{
  for(int i=0; i<4; i++)
  {
    vol_value[i] = 1.0 * data[i] / 32768 * base_vol;
  }
}

